package main

//请求body参数获取
import (
	"bytes"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
)

func main() {
	r := gin.Default()

	/**
	POST http://localhost:8080/stu?age=12&name=12&name=iao&age=22&name=11222&name=tong
	{"name":"tb","age":"23"}
	*/
	r.POST("/stu", func(context *gin.Context) {

		bodyBytes, err := ioutil.ReadAll(context.Request.Body)

		//如果想在此之上获取post中的内容需要重写
		context.Request.Body=ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
		name := context.PostForm("name")
		var age = context.DefaultPostForm("age", "defaultAge")
		context.String(http.StatusOK, "%s,%s", name, age)



		if err != nil {
			context.String(http.StatusBadRequest, err.Error())
			context.Abort()
		} else {
			context.String(http.StatusOK, string(bodyBytes))
		}
	})
	r.Run()
}
